var config = require('../../config');
var util = require('../../framework/util');
var mongoose = require('../../framework/mongoose');

var path = require('path');
var fs = require('fs');
var uuid = require('node-uuid');

var Theme = require('./model').Theme;
var Template = require('./model').Template;
var TemplateLayout = require('./model').TemplateLayout;

//Rest Interface
exports.queryTheme = function (req, res) {
    Theme.find().sort({'ordr': 1}).exec(function (err, themes) {
        res.send({code: 1, data: themes});
    })
};
exports.findThemeById = function(req, res){
    mongoose.findById(Theme, req.params.id, function(obj) {
        Skin.find({theme: obj.id}).sort('order').exec(function (err, skins) {
            res.send({code: 1, data: {theme: obj, skins: skins}});
        });
    });
};

exports.queryTemplateByComponent = function (req, res) {
    Template.find({component: req.query.component}).exec(function (err, templates) {
            res.send({code: 1, data: {templates: templates}});
    });
};
exports.findAllTemplates = function (req, res) {
    Template.find().sort({'ordr': 1}).exec(function (err, templates) {
        res.send({code: 1, data: templates});
    })
};
exports.findTemplateById = function(req, res){
    mongoose.findById(Template, req.params.id, function(obj) {
        TemplateLayout.find({theme: obj.id}).sort('order').exec(function (err, layouts) {
            res.send({code: 1, data: {template: obj, skins: layouts}});
        });
    });
};

